package com.lzw.controller;

import com.lzw.pojo.Student;
import com.lzw.service.StudentService;
import com.lzw.until.PageUntil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/StudentController")
public class StudentController {
    @Autowired
    private StudentService ss;
    @GetMapping("/getAllStu/{bo}")
    public String getAllStu(@PathVariable String bo, Model model, HttpSession session){
        //调用业务
        List<Student> stus=ss.getAllStu();
        model.addAttribute("stus",stus);
        model.addAttribute("bo",bo);
        //把当前视图保存到session
        session.setAttribute("view","getAll");
        return "success";
    }
    @GetMapping("/delStus")
    public String delStus(String ids,HttpSession session){
      boolean bo=ss.delAll(ids);
        String view=(String) session.getAttribute("view");
        if("getAll".equals(view)){
            return "redirect:/StudentController/getAllStu/"+bo+"del";
        }else if("getPage".equals(view)){
            return "redirect:/StudentController/getStuPage/1/"+bo+"del";
        }
        return null;
    }
    @PostMapping("/addStu")
    public String addStu(Student student,HttpSession session){
        boolean bo=ss.addStu(student);
        String view=(String) session.getAttribute("view");
        if("getAll".equals(view)){
            return "redirect:/StudentController/getAllStu/"+bo+"add";
        }else if("getPage".equals(view)){
            return "redirect:/StudentController/getStuPage/1/"+bo+"add";
        }
        return null;
    }
    @GetMapping("/getStuByNo/{stuId}")
    //根据学号查询
    public String getStuByNo(@PathVariable Integer stuId,
                                           Integer pageNow,Integer stuNo,String stuName,
                             Integer cid ,Model model,HttpSession session){
         Student stu=ss.getStuByNo(stuId);
         model.addAttribute("stu",stu);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("stuNo",stuNo);
        model.addAttribute("stuName",stuName);
        model.addAttribute("cid",cid);

        return "editStu";
    }
    @PostMapping("/updateStu")
    //定义一个修改学生信息的方法
    public String updateStu(Student stu,
                            Integer pageNow,String stuId,String stuName1,Integer cid,
                            HttpSession session) throws UnsupportedEncodingException {
        boolean bo=ss.updateStu(stu);
        String view=(String) session.getAttribute("view");
        if("getAll".equals(view)){
            return "redirect:/StudentController/getAllStu/"+bo+"edit";
        }else if("getPage".equals(view)){
            return "redirect:/StudentController/getStuPage/"+pageNow+"/"+bo+"edit?stuNo="+stuId+"&stuName="+ URLEncoder.encode(stuName1,"utf8") +"&cid="+cid;
        }
        return null;
    }
    @GetMapping("/getStuPage/{pageNow}/{bo}")//get比post用的多
    public String getStuPage(@PathVariable Integer pageNow,
                             @PathVariable String bo,
                             Integer stuNo,String stuName,Integer cid,PageUntil page, Map<String,Object> map,Model model,HttpSession session){
        model.addAttribute("bo",bo);
        //判断当前页是否是第一页
        if(pageNow==null){
            pageNow=1;
        }
        if("null".equals(stuNo)){
            stuNo = null;
        }
        //把当前页赋给page
        page.setPageNow(pageNow);
        //把起始下标和偏移量放到map集合
        map.put("index",(pageNow-1)*page.getPageSize());
        map.put("pageSize",page.getPageSize());
        //判断映射的查询条件是否为空
            map.put("stuNo",stuNo);
            map.put("stuName",stuName);
            map.put("cid",cid);
        //把当前视图保存到session
        session.setAttribute("view","getPage");
        //调用业务
        page = ss.getStuPage(map,page);
        //存到model中
        model.addAttribute("page",page);//model存不能用重定向，用请求转发
        model.addAttribute("map",map);
        return "getStuPage";
    }
    @GetMapping("/delStu/{stuId}")
    public String delStu(@PathVariable Integer stuId,Integer pageNow,String stuNo,String stuName,Integer cid, HttpSession session) throws UnsupportedEncodingException {
      boolean bo=ss.delStu(stuId);
      //跳转页面
       String view=(String) session.getAttribute("view");
       if("getAll".equals(view)){
           return "redirect:/StudentController/getAllStu/aa"+bo+"del";
       }else if("getPage".equals(view)){
           return "redirect:/StudentController/getStuPage/"+pageNow+"/"+bo+"del?stuNo="+stuNo+"&stuName="+ URLEncoder.encode(stuName,"utf8") +"&cid="+cid;

       }
       return null;
    }
}
